package com.yx.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 跨域处理
 * */
@Configuration
public class MyCorsConfig {
    @Bean
//    拦截器
    public CorsFilter corsFilter(){
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.addAllowedOrigin("http://localhost:8088");//如果允许所有的跨域，就写*，一般为了安全是写成具体的
        configuration.addAllowedOrigin("http://localhost:8080");//如果允许所有的跨域，就写*，一般为了安全是写成具体的
        configuration.setAllowCredentials(true);//允许传coookie
        configuration.addAllowedMethod("*");//允许什么样的方法，get，post,put等，*表示全部
        configuration.addAllowedHeader("*");//允许请求头，所有的请求头
        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**",configuration);

        return new CorsFilter(urlBasedCorsConfigurationSource);
    }
}
